package cn.edu.nju.askme.action;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.edu.nju.askme.model.User;
import cn.edu.nju.askme.service.IUserService;

import com.opensymphony.xwork2.ActionSupport;
@Component
public class UserRegisterAction extends ActionSupport{
	private User user;
	private String rePassword;
	private IUserService userService;
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public String getRePassword() {
		return rePassword;
	}
	public void setRePassword(String rePassword) {
		this.rePassword = rePassword;
	}
	public IUserService getUserService() {
		return userService;
	}
	@Resource
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	@Override
	public String execute() throws Exception {
		//格式
/*		if(false) {
			this.addFieldError("format", "格式不正确");
			return ERROR;
		}*/
		//密码重复错误
		if(!user.getPassword().equals(rePassword)) {
			System.out.println(user.getPassword());
			System.out.println(rePassword);
			this.addFieldError("rePassword", "密码重复不正确");
			System.out.println("密码重复不正确");
			return ERROR;
		}
		
		//已注册
		if(userService.check(user.getEmail())!=null) {
			this.addFieldError("email", "邮箱已注册");
			System.out.println("邮箱已注册");
			return ERROR;
		}
		if(userService.check(user.getUsername())!=null) {
			this.addFieldError("username", "用户名已注册");
			System.out.println("用户名已注册");
			return ERROR;
		}
		userService.add(user);
		return SUCCESS;
	}
}
